package com.hnqcgczy.shop.dao.impl;

import com.hnqcgczy.shop.dao.IProductDao;
import com.hnqcgczy.shop.db.MyTransaction;
import com.hnqcgczy.shop.vo.TProduct;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ProductDaoimpl implements IProductDao {
    private MyTransaction myTransaction;

    public ProductDaoimpl(MyTransaction myTransaction) {
        this.myTransaction = myTransaction;
    }

    @Override
    public List<TProduct> listAll() throws SQLException {
        Connection conn = myTransaction.getConn();
        //PreparedStatement向数据库发送sql语句
        String sql = "select *from  t_product ";
        PreparedStatement ps = conn.prepareStatement(sql);
        //执行sql语句
        ResultSet rs = ps.executeQuery();
        List<TProduct>list=new ArrayList<>();
        //将查询到的虚拟表中的多条数据放入集合中
        while (rs.next()){
            TProduct tProduct = new TProduct();
            tProduct.setTpcId(rs.getInt(1));
            tProduct.setTpName(rs.getString(2));
            tProduct.setTpDescription(rs.getString(3));
            tProduct.setTpPrice(rs.getDouble(4));
            tProduct.setTpStock(rs.getInt(5));
            tProduct.setTpcId(rs.getInt(6));
            tProduct.setTpcChildId(rs.getInt(7));
            tProduct.setTpFileName(rs.getString(8));
            //每循环一次，将实体类对象放入集合
            list.add(tProduct);
        }
        //释放资源
        rs.close();
        ps.close();
        myTransaction.closeconn();

        return list;
    }

    @Override
    public List<TProduct> querAny(TProduct product, int page, int pageSize) throws SQLException{
            Connection connection=myTransaction.getConn();
            StringBuffer stringBuffer=new StringBuffer(" select * from t_product where 1=1 ");
            if (!product.getTpName().equals("")){
                stringBuffer.append(" and tp_name like ?");
            }
            if (!product.getTpDescription().equals("")){
                stringBuffer.append(" and tp_description like ? ");
            }
            stringBuffer.append(" limit ?,? ");
            PreparedStatement ps=connection.prepareStatement(stringBuffer.toString());
            ps.setString(1,"%"+product.getTpName()+"%");
            ps.setString(2,"%"+product.getTpDescription()+"%");
            ps.setInt(3,(page-1)*pageSize);
            ps.setInt(4,pageSize);
            ResultSet rs=ps.executeQuery();
            List<TProduct> list=new ArrayList<>();
        while (rs.next()) {
            TProduct tProduct = new TProduct();
            tProduct.setTpcId(rs.getInt(1));
            tProduct.setTpName(rs.getString(2));
            tProduct.setTpDescription(rs.getString(3));
            tProduct.setTpPrice(rs.getDouble(4));
            tProduct.setTpStock(rs.getInt(5));
            tProduct.setTpcId(rs.getInt(6));
            tProduct.setTpcChildId(rs.getInt(7));
            tProduct.setTpFileName(rs.getString(8));
            list.add(product);
        }
        rs.close();
        ps.close();
        myTransaction.closeconn();
        return list;
    }

    @Override
    public TProduct querByID(int tp_id) throws SQLException {
        //获得数据库连接
        // MyTransaction myTransaction=new MyTransaction(true);
        Connection conn = myTransaction.getConn();
        //PreparedStatement向数据库发送sql语句
        String sql = "select *from  t_product where tp_id=?";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setInt(1, tp_id);
        //执行sql语句
        ResultSet rs = ps.executeQuery();
        TProduct tProduct =null;
        if (rs.next()) {
             tProduct = new TProduct();
            tProduct.setTpcId(rs.getInt(1));
            tProduct.setTpName(rs.getString(2));
            tProduct.setTpDescription(rs.getString(3));
            tProduct.setTpPrice(rs.getDouble(4));
            tProduct.setTpStock(rs.getInt(5));
            tProduct.setTpcId(rs.getInt(6));
            tProduct.setTpcChildId(rs.getInt(7));
            tProduct.setTpFileName(rs.getString(8));
        }
        rs.close();
        ps.close();
       myTransaction.closeconn();
        return tProduct;

    }

    @Override
    public boolean deleteById(int tp_id) throws SQLException {
        return false;
    }

    @Override
    public boolean updateById(int tp_id) throws SQLException {
        return false;
    }

    @Override
    public boolean insert(TProduct tProduct) throws SQLException {
        return false;
    }

    public static void main(String[] args) {
       ProductDaoimpl productDaoimpl=new ProductDaoimpl(new MyTransaction(true));
       TProduct t=new TProduct();
       t.setTpName("黄桃");
       t.setTpDescription("炎");
        try {
            List<TProduct> list = productDaoimpl.querAny(t, 2, 3);
            System.out.println(list);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}
